package com.activiti.z_six.controller;

import com.activiti.z_six.SecurityUtil;
import com.activiti.z_six.aspectj.annotation.OperLog;
import com.activiti.z_six.aspectj.operate.LogConst;
import com.activiti.z_six.entity.taskAssignee.FlowElementAttrs;
import com.activiti.z_six.security.RedisUtils;
import com.activiti.z_six.service.IAssigneeUserService;
import com.activiti.z_six.service.IProcessDefinitionService;
import com.activiti.z_six.util.ResultRes;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
public class TaskAssigneeController {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private IProcessDefinitionService processDefinitionService;
    @Autowired
    private RedisUtils redisUtils;

    @OperLog(operModul = "流程任务相关" , operType = LogConst.OTHER , operDesc = "获取用户任务中设置的规则数据")
    @GetMapping("/as/getAssigneeUserWithTaskId")
    public ResultRes getAssigneeUserWithTaskId(HttpServletResponse response,
                                               @RequestParam("usertaskid") String usertaskid,
                                               @RequestParam("ruleName") String ruleName){
        try{
            return ResultRes.success(assigneeUserService.getAssigneeUserWithTaskKey(usertaskid,ruleName));
        }
        catch (Exception ex){
            return ResultRes.error(ex.toString());
        }
    }
    @OperLog(operModul = "流程任务相关" , operType = LogConst.OTHER , operDesc = "设置用户任务中设置的规则数据")
    @PostMapping("/as/setAssigneeUser")
    public ResultRes setAssigneeUser(@RequestBody JSONObject param){
        return ResultRes.success(assigneeUserService.setAssigneeUser(param));
    }
    @OperLog(operModul = "流程任务相关" , operType = LogConst.OTHER , operDesc = "设置用户任务中设置的参数")
    @PostMapping("/as/setFlowElementAttrs")
    public ResultRes setFlowElementAttrs(@RequestBody JSONObject param){
        String redisKey=param.getString("redisKey");
        String attrValue=param.getString("attrKey");
        if(redisUtils.exists(redisKey)){
            redisUtils.remove(redisKey);
        }
        redisUtils.set(redisKey,attrValue);
        return ResultRes.success("设置成功");
    }
    @OperLog(operModul = "流程任务相关" , operType = LogConst.OTHER , operDesc = "获取流程业务配置数据")
    @GetMapping("/as/getFlowElementAttrs")
    public ResultRes getFlowElementAttrs(HttpServletResponse response,
                                         @RequestParam("usertaskid") String usertaskid){
        HashMap<String,Object> hashMap=new HashMap<>();
        FlowElementAttrs flowElementAttrs=processDefinitionService.getFlowElementAttrs(usertaskid);
        if(flowElementAttrs==null) {
            if (redisUtils.exists(usertaskid + "_returnWay")) {
                hashMap.put("returnWay", redisUtils.get(usertaskid + "_returnWay"));
            } else {
                hashMap.put("returnWay", "none");
            }
            if (redisUtils.exists(usertaskid + "_runWay")) {
                hashMap.put("runWay", redisUtils.get(usertaskid + "_runWay"));
            } else {
                hashMap.put("runWay", "none");
            }
            if (redisUtils.exists(usertaskid + "_jumpWay")) {
                hashMap.put("jumpWay", redisUtils.get(usertaskid + "_jumpWay"));
            } else {
                hashMap.put("jumpWay", "none");
            }
            if (redisUtils.exists(usertaskid + "_ccWay")) {
                hashMap.put("ccWay", redisUtils.get(usertaskid + "_ccWay"));
            } else {
                hashMap.put("ccWay", "none");
            }
            if (redisUtils.exists(usertaskid + "_autoCCWay")) {
                hashMap.put("autoCCWay", redisUtils.get(usertaskid + "_autoCCWay"));
            } else {
                hashMap.put("autoCCWay", "none");
            }
            if (redisUtils.exists(usertaskid + "_transfer")) {
                hashMap.put("transfer", redisUtils.get(usertaskid + "_transfer"));
            }
            else{
                hashMap.put("transfer", "0");
            }
            if (redisUtils.exists(usertaskid + "_countersign")) {
                hashMap.put("countersign", redisUtils.get(usertaskid + "_countersign"));
            }
            else{
                hashMap.put("countersign", "0");
            }
            if (redisUtils.exists(usertaskid + "_endTask")) {
                hashMap.put("endTask", redisUtils.get(usertaskid + "_endTask"));
            }
            else{
                hashMap.put("endTask", "0");
            }
            if (redisUtils.exists(usertaskid + "_refuse")) {
                hashMap.put("refuse", redisUtils.get(usertaskid + "_refuse"));
            }
            else{
                hashMap.put("refuse", "0");
            }
        }
        else{
            hashMap.put("returnWay", flowElementAttrs.getReturnWay());
            hashMap.put("runWay", flowElementAttrs.getRunWay());
            hashMap.put("jumpWay", flowElementAttrs.getJumpWay());
            hashMap.put("ccWay", flowElementAttrs.getCcWay());
            hashMap.put("autoCCWay", flowElementAttrs.getAutoCCWay());
            hashMap.put("transfer", flowElementAttrs.getTransfer().toString());
            hashMap.put("countersign", flowElementAttrs.getCountersign().toString());
            hashMap.put("endTask", flowElementAttrs.getEndTask().toString());
            hashMap.put("refuse",flowElementAttrs.getRefuse().toString());
        }
        return ResultRes.success(hashMap);
    }
    @OperLog(operModul = "流程任务相关" , operType = LogConst.OTHER , operDesc = "删除接收人")
    @PostMapping("/as/deleteAssigneeUser")
    public ResultRes deleteAssigneeUser(@RequestBody JSONObject param){
        String usertaskid=param.getString("usertaskid");
        String username=param.getString("username");
        try{
            return ResultRes.success(assigneeUserService.deleteAssigneeUser(usertaskid,username));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
}
